package com.entrogy.service.impl;

import com.entrogy.bean.SchoolBusLine;
import com.entrogy.dao.SchoolbuslineMapper;
import com.entrogy.service.SchoolBusLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Author 许红松
 * @date 2019/4/30 0030 下午 1:52
 * 校车站点service接口实现类
 */
@Service(value="schoolbuslineService")
@Transactional
public class SchoolBusLineServiceImpl implements SchoolBusLineService {

    @Autowired
    private SchoolbuslineMapper schoolbuslineMapper;//校车站点mapper接口

    /**
     * 新增校车站点信息
     * @param schoolBusLine
     * @return int
     */
    @Override
    public int addSchoolBusLine(SchoolBusLine schoolBusLine) {
        return schoolbuslineMapper.addSchoolBusLine(schoolBusLine);
    }

    /**
     * 删除校车站点信息
     * @param schoolbus_Siteid
     * @return int
     */
    @Override
    public int deleteSchoolBusLine(Integer schoolbus_Siteid) {
        return schoolbuslineMapper.deleteSchoolBusLine(schoolbus_Siteid);
    }

    /**
     * 修改校车站点信息
     * @param schoolBusLine
     * @return int
     */
    @Override
    public int updateBusLine(SchoolBusLine schoolBusLine) {
        return schoolbuslineMapper.updateBusLine(schoolBusLine);
    }

    /**
     *  获取校车站点信息
     * @param map
     * @return List<SchoolBusLine>
     */
    @Override
    public List<SchoolBusLine> getSchoolBusLineList(Map<String, Object> map) {
        return schoolbuslineMapper.getSchoolBusLineList(map);
    }

    /**
     *  获取校车站点信息
     * @param map
     * @return
     */
    @Override
    public int count(Map<String, Object> map) {

        return schoolbuslineMapper.count(map);
    }

    /**
     * 跟改位置
     * @return
     */
    @Override
    public int updateplace(Map<String, Object> map) {
        if (map.containsKey("up")){  //上移
            int  step=Integer.parseInt(map.get("line").toString());
            map.put("scSiteid",map.get("schoolbus_Siteid"));
            map.put("line",step-1);
            schoolbuslineMapper.updateplace(map);//先上移以前的数据
            upsins(map);

        }else if(map.containsKey("down")){//下移
            int  step=Integer.parseInt(map.get("line").toString());
            map.put("scSiteid",map.get("schoolbus_Siteid"));
            map.put("line",step+1);
            schoolbuslineMapper.updateplace(map);//先上移以前的数据
            upsins(map);
        }
        return schoolbuslineMapper.updateplace(map);
    }

    /**
     *   公用方法
     * @param map
     */
    public void upsins(Map<String, Object> map){
        String schoolbus_Siteid=map.get("schoolbus_Siteid").toString();
        int  step=Integer.parseInt(map.get("line").toString());
        List<SchoolBusLine>     list=schoolbuslineMapper.getSchoolBusLineList1(map);//查询line数据相同的
        if (map.containsKey("up")) {
            int m = list.size();
            for (int i = 0; i < m; i++) {
                if (!schoolbus_Siteid.equals(list.get(i).getSchoolbusSiteid())){
                    map.put("scSiteid",list.get(i).getSchoolbusSiteid());
                    map.put("line",step);
                    schoolbuslineMapper.updateplace(map);
                }

            }
        }
    }
    /**
     *  获取校车站点信息
     * @param map
     * @return
     */
    @Override
    public List<SchoolBusLine> getSchoolBusLineList1(Map<String, Object> map) {
        return schoolbuslineMapper.getSchoolBusLineList1(map);
    }
}
